package org.example.controller;

import org.example.pojo.UmsRule;
import org.example.service.IUmsRuleRelateService;
import org.example.service.IUmsRuleService;
import org.example.util.CommonResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wy
 * @since 2023-10-04
 */
@RestController
@RequestMapping("/ums-rule")
public class UmsRuleController {
    @Resource
    IUmsRuleService ruleService;

    @Resource
    IUmsRuleRelateService relateService;

    @GetMapping("/right")
    CommonResult getRight(List<String> names){
        UmsRule rule = ruleService.getRuleByParent(names);
        return CommonResult.success(rule.getName());
    }

    @PostMapping("/add")
    CommonResult add(UmsRule rule){
        rule.setActive(1);
        if(ruleService.saveOrUpdate(rule)){
            return CommonResult.success("添加成功");
        }

        return CommonResult.failed("添加失败");
    }

    @PostMapping("/del")
    CommonResult delete(UmsRule rule){
        rule.setActive(0);
        if(ruleService.updateById(rule)){
            relateService.updateByRuleId(rule.getId());
            return CommonResult.success("删除成功");
        }

        return CommonResult.failed("删除失败");
    }

    @PostMapping("/batchdel")
    CommonResult batchdel(Integer[] ids){
        return CommonResult.success(ruleService.batchdel(ids));
    }

    @PostMapping("/update")
    CommonResult update(UmsRule rule){
        if(ruleService.updateById(rule)){
            return CommonResult.success("修改成功");
        }

        return  CommonResult.failed("修改失败");
    }

    @GetMapping("/list")
    CommonResult list(UmsRule rule){
        return CommonResult.success(ruleService.list(rule));
    }

    @PostMapping("/back")
    CommonResult back(UmsRule rule){
        rule.setActive(1);
        return CommonResult.success(ruleService.updateById(rule));
    }

}
